const cloud = require('wx-server-sdk')
cloud.init({
    env: cloud.DYNAMIC_CURRENT_ENV
})

exports.main = async (event, context) => {
    try {
        // 获取文件ID
        const fileID = event.fileID
        if (!fileID) {
            throw new Error('No fileID provided')
        }

        // 获取文件信息
        const fileInfo = await cloud.getTempFileURL({
            fileList: [fileID]
        })

        if (!fileInfo.fileList || fileInfo.fileList.length === 0) {
            throw new Error('File not found')
        }

        return {
            code: 0,
            message: '处理成功',
            data: {
                fileID: fileID,
                tempFileURL: fileInfo.fileList[0].tempFileURL,
                downloadUrl: fileInfo.fileList[0].download_url
            }
        }
    } catch (err) {
        return {
            code: -1,
            message: err.message,
            data: null
        }
    }
}